
/**		
 * 
 *	uk.co.lmfm.guppy.view.SettingsMediator
 *	
 *	@version 1.00 | Jan 12, 2010
 *	@author Justin Windle
 *  
 **/
 
package uk.co.lmfm.guppy.view 
{
	import uk.co.lmfm.guppy.model.SettingsProxy;
	import uk.co.lmfm.guppy.application.enum.ApplicationConstants;
	import uk.co.lmfm.guppy.model.vo.SettingsVO;
	import uk.co.lmfm.guppy.model.vo.ViewVO;
	import uk.co.lmfm.guppy.view.components.events.SettingsViewEvent;
	import uk.co.lmfm.guppy.view.components.settings.SettingsView;

	import org.puremvc.as3.multicore.interfaces.IMediator;
	import org.puremvc.as3.multicore.interfaces.INotification;
	import org.puremvc.as3.multicore.patterns.mediator.Mediator;

	/**
	 * SettingsMediator
	 */
	public class SettingsMediator extends Mediator implements IMediator 
	{
		//	----------------------------------------------------------------
		//	CONSTANTS
		//	----------------------------------------------------------------

		public static const NAME : String = "SettingsMediator";

		//	----------------------------------------------------------------
		//	CONSTRUCTOR
		//	----------------------------------------------------------------

		public function SettingsMediator(viewComponent : SettingsView)
		{
			super(NAME, viewComponent);
		}

		//	----------------------------------------------------------------
		//	PUBLIC METHODS
		//	----------------------------------------------------------------

		override public function onRegister() : void
		{
			super.onRegister();
			component.addEventListener(SettingsViewEvent.SAVE_SELECTED, onComponentSaveSelected);			component.addEventListener(SettingsViewEvent.AUTO_SETUP_REQUESTED, onAutosetupRequested);			component.addEventListener(SettingsViewEvent.AUTO_SETUP_CANCELLED, onAutosetupCancelled);
		}

		override public function listNotificationInterests() : Array
		{
			return [
					ApplicationConstants.CHANGE_VIEW,					ApplicationConstants.GOT_USER_SETTINGS,
					ApplicationConstants.POLL_VIEWS_FOR_DEFINITIONS,
					ApplicationConstants.AUTO_SETUP_START,
					ApplicationConstants.AUTO_SETUP_PROGRESS,					ApplicationConstants.AUTO_SETUP_COMPLETE
					];
		}

		override public function handleNotification(notification : INotification) : void
		{
			super.handleNotification(notification);
			
			var name : String = notification.getName();
			var body : Object = notification.getBody();
			
			switch(name)
			{
				case ApplicationConstants.CHANGE_VIEW : 
				
					if(body == NAME)
					{
						sendNotification(ApplicationConstants.GET_USER_SETTINGS);
						component.show();
					}
					else
					{
						component.hide();
					}
				
					break;
					
				case ApplicationConstants.GOT_USER_SETTINGS :
				
					component.settings = SettingsVO(body);
				
					break;
					
				case ApplicationConstants.POLL_VIEWS_FOR_DEFINITIONS :
				
					var definition : ViewVO = new ViewVO();
			
					definition.name = SettingsMediator.NAME;
					definition.label = "Settings";
			
					sendNotification(ApplicationConstants.GOT_VIEW_DEFINITION, definition);
			
					break;
				
				case ApplicationConstants.AUTO_SETUP_START :
				
					component.showAutoSetupMessage();
				
					break;
					
				case ApplicationConstants.AUTO_SETUP_PROGRESS :
				
					component.settings = body as SettingsVO;
				
					break;
					
				case ApplicationConstants.AUTO_SETUP_COMPLETE :
				
					component.settings = body as SettingsVO;
				
					break;
			}
		}

		//	----------------------------------------------------------------
		//	EVENT HANDLERS
		//	----------------------------------------------------------------

		private function onComponentSaveSelected(event : SettingsViewEvent) : void
		{
			sendNotification(ApplicationConstants.SET_USER_SETTINGS, component.settings);
		}
		
		private function onAutosetupRequested(event : SettingsViewEvent) : void
		{
			sendNotification(ApplicationConstants.AUTO_SETUP_REQUEST);
		}

		private function onAutosetupCancelled(event : SettingsViewEvent) : void
		{
			var settingsProxy : SettingsProxy = facade.retrieveProxy(SettingsProxy.NAME) as SettingsProxy;
			settingsProxy.cancelAutoSetup();
		}

		//	----------------------------------------------------------------
		//	PUBLIC ACCESSORS
		//	----------------------------------------------------------------

		public function get component() : SettingsView
		{
			return viewComponent as SettingsView;
		}
	}
}
